<?php
namespace app\common\model;


use think\Db;
use think\Model;

class Setting extends Model{


    protected $table = "jiujiu_system_setting";



    public function getRule($name){

       $data =  $this->where('name',$name)->column('value');
       if($data){
           return $data[0];
       }
        return '';
    }


    public function getPromotionRule(){
        $data = [];
        for($i=1;$i<=9;$i++){
            $k = "system_shop_buy_num".$i;
            $data[$k] = $this->getRule($k);
        }

        for($i=1;$i<=9;$i++){
            $k = "system_shop_buy_price".$i;
            $data[$k] = $this->getRule($k);
        }

        for($i=1;$i<=9;$i++){
            $k = "father_shop_buy_num".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "system_shop_buy_price".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "team_num".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "grade_num".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "promotion_credit".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "system_shop_get_order".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "system_shop_get_friends".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "system_shop_get_paidan".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "system_shop_get_credit".$i;
            $data[$k] = $this->getRule($k);
        }
        for($i=1;$i<=9;$i++){
            $k = "send_order_to".$i;
            $data[$k] = $this->getRule($k);
        }

        $data['open_shop_num'] = $this->getRule('open_shop_num');
        $data['add_product_num'] = $this->getRule('add_product_num');



       return $data;
    }



    public function getCreditRule(){

        return $this->getRule('consume_credit');

    }

   public function getPayRule(){

       $data['wxappid'] =  $this->getRule('wxappid');
       $data['wxpaysignkey'] =  $this->getRule('wxpaysignkey');
       $data['wxmchid'] =  $this->getRule('wxmchid');
       $data['cert_path'] =  $this->getRule('cert_path');
       $data['key_path'] =  $this->getRule('key_path');
       return $data;
    }


    public function getProfitRule(){
        return $this->getRule('min_profit');
    }



    public function savePromotionRule($data,$cate){
        $name = array_keys($data);
        $value = array_values($data);
        $arr = [];
        foreach ($name as $k=>$v){
            $arr[$k]['name'] = $v;
            $arr[$k]['value'] = $value[$k];
            $arr[$k]['createTime'] = time();
            $arr[$k]['cate'] = $cate;
        }
        $this->where('cate',$cate)->delete();
        $this->insertAll($arr);
    }







}